﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Things.Web.SaleSet
{
    public partial class AddSaleSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _ctlHideGoodsId.Value = Request.QueryString["GoodsId"];
                _ctlHideReferrer.Value = Request.UrlReferrer.ToString();

                Guid goodsId = new Guid(_ctlHideGoodsId.Value);

                string type = Request.QueryString["type"];
                if (type.ToUpper() == "A")
                {
                    this._ctlEditCaption.Text = "体验式A计划 即时订购";
                    this._ctlEditDescription.Text = "A";
                    _ctlEditCycle.Text = "1";
                }
                else if (type.ToUpper() == "B")
                {
                    this._ctlEditCaption.Text = "体验式B计划 双季订购";
                    this._ctlEditDescription.Text = "B";
                    _ctlEditCycle.Text = "6";
                }
                else if(type.ToUpper() == "C")
                {
                    this._ctlEditCaption.Text = "深度式C计划 全年订购";
                    this._ctlEditDescription.Text = "C";
                    _ctlEditCycle.Text = "12";
                }

                Things.BLL.GoodsPrice bll = new Things.BLL.GoodsPrice();
                StringBuilder strWhere = new StringBuilder();
                strWhere.Append(" GoodsID='" + goodsId.ToString() + "'");
                List<Things.Model.GoodsPrice> models = bll.GetModelList(strWhere.ToString());
                if (models != null && models.Count>0)
                {
                    this._ctlEditCurrentPrice.Text = models[0].GoodsCurrentPrice.ToString();
                    this._ctlEditStandardPrice.Text = models[0].GoodsStandPrice.ToString();
                }

                Things.OrderPlan.Models.SaleSet set = new Things.OrderPlan.SaleSetRepository().Find(goodsId, this._ctlEditDescription.Text);
                if (set != null)
                {
                    _ctlEditNumber.Text = set.DeliveredNumber.ToString();
                    _ctlEditCycle.Text = set.DeliveredCycle.ToString();
                    _ctlEditCount.Text = set.DeliveredCount.ToString();
                    _ctlEditUnitPrice.Text = set.UnitPrice.ToString();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Guid goodsId = new Guid(_ctlHideGoodsId.Value);
            Things.OrderPlan.Models.SaleSet set = new Things.OrderPlan.SaleSetRepository().Find(goodsId, this._ctlEditDescription.Text);
            if (set == null)
            {
                set = new OrderPlan.Models.SaleSet();
                set.Id = Guid.NewGuid();
                set.GoodsId = new Guid(_ctlHideGoodsId.Value);
                set.Caption = _ctlEditCaption.Text;
                set.Description = _ctlEditDescription.Text;
            }

            set.DeliveredNumber = Convert.ToInt32(_ctlEditNumber.Text);
            set.DeliveredCycle = Convert.ToInt32(_ctlEditCycle.Text);
            set.DeliveredCount = Convert.ToInt32(_ctlEditCount.Text);
            set.UnitPrice = Convert.ToDouble(_ctlEditUnitPrice.Text);

            Things.OrderPlan.SaleSetRepository repository = new OrderPlan.SaleSetRepository();
            if (repository.Restore(set))
            {
                Response.Write("<script>alert('添加成功!');window.location.href='" + _ctlHideReferrer.Value + "';</script>");
            }
            else
            {
                Response.Write("<script>alert('添加失败!');</script>");
            }
        }

        protected void _ctlBtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(_ctlHideReferrer.Value);
        }
    }
}